local lsp_tianji = fk.CreateSkill {
    name = "lsp_tianji",
}

Fk:loadTranslationTable {
    ["lsp_tianji"] = "天机",
    [":lsp_tianji"] = "每当你的装备牌进入弃牌堆或被其他角色获得时，你可以令攻击范围内的至多两名其他角色于下次受到的伤害+1。",

    ["@tianji_sun"] = "天机",
}
lsp_tianji:addEffect(fk.AfterCardsMove, {
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(lsp_tianji.name) then return end
        for _, move in ipairs(data) do
            if move.from == player and move.to and move.to ~= player and move.moveReason == fk.ReasonPrey then
                for _, info in ipairs(move.moveInfo) do
                    if Fk:getCardById(info.cardId).type == Card.TypeEquip and (info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand) then
                        return true
                    end
                end
            elseif move.toArea == Card.DiscardPile and move.from == player then
                for _, info in ipairs(move.moveInfo) do
                    if Fk:getCardById(info.cardId).type == Card.TypeEquip and (info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand) and table.contains(player.room.discard_pile, info.cardId) then
                        return true
                    end
                end
            end
        end
    end,
    trigger_times = function(self, event, target, player, data)
        local i = 0
        for _, move in ipairs(data) do
            if move.from == player and move.to and move.to ~= player and move.moveReason == fk.ReasonPrey then
                for _, info in ipairs(move.moveInfo) do
                    if Fk:getCardById(info.cardId).type == Card.TypeEquip and (info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand) then
                        i = i + 1
                    end
                end
            elseif move.toArea == Card.DiscardPile and move.from == player then
                for _, info in ipairs(move.moveInfo) do
                    if Fk:getCardById(info.cardId).type == Card.TypeEquip and (info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand) and table.contains(player.room.discard_pile, info.cardId) then
                        i = i + 1
                    end
                end
            end
        end
        return i
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local tos = room:askToChoosePlayers(player, {
            min_num = 1,
            max_num = 2,
            targets = table.filter(room:getOtherPlayers(player), function(p) return player:inMyAttackRange(p) end),
            skill_name = lsp_tianji.name,
            prompt = "天机：你可以令至多两名攻击范围内的角色于下次受到伤害时，伤害值额外+1",
            cancelable = true,
        })
        if #tos > 0 then
            room:sortByAction(tos)
            event:setCostData(self, { tos = tos })
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        for _, to in ipairs(event:getCostData(self).tos) do
            room:addPlayerMark(to, "@tianji_sun")
        end
    end,
})
lsp_tianji:addEffect(fk.DamageInflicted, {
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:getMark("@tianji_sun") > 0
    end,
    on_use = function(self, event, target, player, data)
        data:changeDamage(player:getMark("@tianji_sun"))
        player.room:setPlayerMark(player, "@tianji_sun", 0)
    end,
})
return lsp_tianji
